<template>
  <div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="patentName" label="专利名称" width="180" />
      <el-table-column prop="patentProperty" label="专利属性" width="180" />
      <el-table-column prop="patentCategory" label="专利类别" width="180" />
      <el-table-column prop="authorizationStatus" label="授权状态" width="180" />
      <el-table-column prop="applicationNumber" label="申请号" width="180" />
      <el-table-column prop="publicationNumber" label="公开号" width="180" />
      <el-table-column prop="authorizationNumber" label="授权号" width="180" />
      <el-table-column prop="inventor" label="发明人" width="180" />
      <el-table-column prop="patentHolder" label="专利权人" width="180" />
      <el-table-column prop="applicationDate" label="申请日期" width="180" />
      <el-table-column prop="authorizationDate" label="授权日期" width="180" />
      <el-table-column prop="patentCertificate" label="专利证书" width="180" />
      <el-table-column prop="reportDate" label="填报日期" width="180" />
      <el-table-column prop="auditStatus" label="审核状态" width="180" />
      <el-table-column prop="feedback" label="反馈原因" width="180" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button round type="warning" @click="handleUpdate(scope.row)">审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogUpdateVisible" title="审核专利">
      <el-form ref="updateFormRef" :model="updateForm" :rules="rules" label-width="100px" class="ruleForm" status-icon>
        <el-form-item label="专利名称" prop="patentName">
          <el-input v-model="updateForm.patentName"/>
        </el-form-item>
        <el-form-item label="专利属性" prop="patentProperty">
          <el-input v-model="updateForm.patentProperty"/>
        </el-form-item>
        <el-form-item label="专利类别" prop="patentCategory">
          <el-input v-model="updateForm.patentCategory"/>
        </el-form-item>
        <el-form-item label="授权状态" prop="authorizationStatus">
          <el-input v-model="updateForm.authorizationStatus"/>
        </el-form-item>
        <el-form-item label="申请号" prop="applicationNumber">
          <el-input v-model="updateForm.applicationNumber"/>
        </el-form-item>
        <el-form-item label="公开号" prop="publicationNumber">
          <el-input v-model="updateForm.publicationNumber"/>
        </el-form-item>
        <el-form-item label="授权号" prop="authorizationNumber">
          <el-input v-model="updateForm.authorizationNumber"/>
        </el-form-item>
        <el-form-item label="发明人" prop="inventor">
          <el-input v-model="updateForm.inventor"/>
        </el-form-item>
        <el-form-item label="专利权人" prop="patentHolder">
          <el-input v-model="updateForm.patentHolder"/>
        </el-form-item>
        <el-form-item label="申请日期" prop="applicationDate">
          <el-date-picker v-model="updateForm.applicationDate" type="date" placeholder="选择日期"/>
        </el-form-item>
        <el-form-item label="授权日期" prop="authorizationDate">
          <el-date-picker v-model="updateForm.authorizationDate" type="date" placeholder="选择日期"/>
        </el-form-item>
        <el-form-item label="专利证书" prop="patentCertificate">
          <el-input v-model="updateForm.patentCertificate"/>
        </el-form-item>
        <el-form-item label="填报日期" prop="reportDate">
          <el-date-picker v-model="updateForm.reportDate" type="date" placeholder="选择日期"/>
        </el-form-item>
        <el-form-item label="审核状态" prop="auditStatus">
          <el-select v-model="updateForm.auditStatus" placeholder="请选择">
            <el-option label="通过" value="通过"></el-option>
            <el-option label="不通过" value="不通过"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="反馈原因" prop="feedback" v-if="updateForm.auditStatus === '不通过'">
          <el-input v-model="updateForm.feedback"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogUpdateVisible=false">取消</el-button>
          <el-button type="primary" @click="handleUpdateConfirm">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { onMounted, reactive, ref, watch } from "vue";
import axios from "axios";

const tableData = ref([]);

const getList = async (query = "") => {
  try {
    const res = query ?
        await axios.get(`http://localhost:8000/adminapi/patents?patentName=${query}`) :
        await axios.get('http://localhost:8000/adminapi/patents');
    tableData.value = res.data;
  } catch (error) {
    console.error('Error fetching data:', error);
  }
};

const rules = reactive({
  patentName: [{ required: true, message: '请输入专利名称', trigger: 'blur' }],
  auditStatus: [{ required: true, message: '请选择审核状态', trigger: 'change' }],
  feedback: [{ required: false, message: '请输入反馈原因', trigger: 'blur' }]
});

onMounted(() => {
  getList();
});

const dialogUpdateVisible = ref(false);
const updateFormRef = ref();
const currentItem = ref();
const updateForm = reactive({
  patentName: "",
  patentProperty: "",
  patentCategory: "",
  authorizationStatus: "",
  applicationNumber: "",
  publicationNumber: "",
  authorizationNumber: "",
  inventor: "",
  patentHolder: "",
  applicationDate: "",
  authorizationDate: "",
  patentCertificate: "",
  reportDate: "",
  auditStatus: "",
  feedback: ""
});

watch(() => updateForm.auditStatus, (newValue) => {
  rules.feedback[0].required = newValue === '不通过';
});

const handleUpdate = (item) => {
  dialogUpdateVisible.value = true;
  Object.assign(updateForm, item);
  currentItem.value = item;
};

const handleUpdateConfirm = async () => {
  try {
    await updateFormRef.value.validate(async (valid) => {
      if (valid) {
        dialogUpdateVisible.value = false;
        if (currentItem.value.id !== undefined) {
          await axios.put(`http://localhost:8000/adminapi/patents/${currentItem.value.id}`, updateForm);
          await getList();
        } else {
          console.error('Patent ID is undefined');
        }
      }
    });
  } catch (error) {
    console.error('Error updating patent:', error.response.data);
  }
};
</script>
